<script>
	import {location} from "@/utils/geo.js"
	import storage from "@/utils/storage.js"
	import {registerTravelTimer,clearTravelTimer} from "@/utils/timer/travelTimer.js"
	import {registerPositionTimer,clearPositionTimer} from "@/utils/timer/positionTimer.js"
	import {REFERSH_TRAVEL_TIMER, REFRESH_INDEX,REFRESH_TRAVELS,REFRESH_CALENDAR,STOP_SERVICE} from "@/core/constant/event.js"
	import {SERVICE_START,SERVICE_STOP} from "@/core/constant/volunteer.js"
	import * as Role from "@/core/constant/role.js"
	import {CURRENT_TRAVEL} from "@/store/mutations_type.js"
	import {TRAVEL_DONE } from "@/core/constant/travel.js"
	import {navToTravel,checkRole} from "@/core/app.js"
	import * as TravelApi from "@/api/travel/travel.js"
	import * as VolunteerApi from "@/api/assist/volunteer.js"
	
	export default {
		methods:{
			// 获取支付宝的认证code
			getAuthCode(){
				return new Promise((resolve,reject) =>{
					my.getAuthCode({
						scopes: 'auth_user',//主动授权（弹框）：auth_user，静默授权（不弹框）：auth_base
						success: res => {
						    resolve(res.authCode)
						},
						fail:reject
					});
				})
			},
			// 向后端发起登录请求
			async login(){
				return this.$store.dispatch("user/login",{code: await this.getAuthCode()})
			},
			/**
			 * 获取待出行列表
			 */
			async getInactiveTravels(){
				return TravelApi.inactiveTravelList()
			},
			/**
			 * 判断当前是否有处于TRAVEL_GOING的出行
			 */
			initGoingTravel(){
				console.log("---initGoingTravel---")
				const currentTravel = storage.get(CURRENT_TRAVEL)
				if(currentTravel && currentTravel.status !== TRAVEL_DONE){
					this.$store.dispatch("travel/setCurrentTravel",currentTravel)
					navToTravel()
				}
			},
			async init(){
				try{
					// 1. 登录
					await this.login()
					this.$success("登录成功！")
					// 2. 初始化ws连接
					if(this.$store.getters.role !== Role.ROLE_UNDEFINED){
						this.$store.dispatch("socket/openSocket")
					}
					// 3. 若有正在进行的出行，则跳转到正在出行页面
					this.initGoingTravel()
					// 4. 获取未出行列表，开启出行定时
					await this.refreshTravelTimer()
					// 5. 刷新首页
					uni.$emit(REFRESH_INDEX)
					// 6. 开启定位
					registerPositionTimer()
				}catch(err) {
					console.log(err)
				}
				
			},
			async clearServiceStatus(){
				if(checkRole() !== Role.ROLE_VOLUNTEER ){
					return
				}
				const volunteerService = this.$store.getters.volunteerService
				if(volunteerService.status === SERVICE_START){
					const params = {
						travelWaitId:volunteerService.travelWaitId
					}
					await VolunteerApi.stopService(params)
					
				}
			},
			async refreshTravelTimer(){
				console.log("----------refreshTravelTimer----------")
				const recentTravels = await this.getInactiveTravels()
				this.$store.dispatch("travel/clearTravels")
				registerTravelTimer(recentTravels)
				uni.$emit(REFRESH_TRAVELS)
				uni.$emit(REFRESH_CALENDAR)
			}
		},
		onLaunch:function() {
			this.$speak("/static/global/welcome.mp3")
			this.init()
			uni.$on(REFERSH_TRAVEL_TIMER,this.refreshTravelTimer)
		},
		onShow:function() {
		},
		onHide: async function() {
			console.log('App Hide')
			uni.$emit(STOP_SERVICE)
		},
		onExit:async function(){
			clearPositionTimer()
			uni.$emit(STOP_SERVICE)
		}
		
		
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	@import "@/uni_modules/uview-ui/index.scss";
	@import "@/static/css/common.scss";
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';
	// 设置整个项目的背景色
	page {
		background-color: #f5f5f5;
	}
	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}
	
	
	
	.uni-toast{
	  background-color: #fff7f8;
	  box-shadow: 0 0 10rpx pink;
	  width: 160rpx;
	  .uni-loading{
	    background-image: none;
	    border-radius: 50%;
	    border-top: 16rpx pink solid;
	  }
	}
	 [hidden]{display: none !important;}
</style>
